﻿using Microsoft.SystemCenter.Orchestrator.Integration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GenerateReports
{
    [Activity("Runbook Report")]
    public class RunbookReport : ReportBase, IActivity
    {
        public void Design(IActivityDesigner designer)
        {
            string RunbooksUrl = config.ServiceUrl + "Runbooks";
            string data = Util.GetServiceData(RunbooksUrl);
            List<string> runbooks = Util.FindPropertyValueCollection(data, "Name");
            runbooks.Sort();

            designer.AddInput("Runbook Name").WithListBrowser(runbooks);
            designer.AddInput("Report Path").WithFileBrowser();
        }

        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            string runbookname = request.Inputs["Runbook Name"].AsString();
            string path = request.Inputs["Report Path"].AsString();
            string webresponse;
            string url;
            List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
            
            // get runbook Id
            url = config.ServiceUrl;
            url += "Runbooks?$filter=Name eq '" + runbookname + "'";
            webresponse = Util.GetServiceData(url);
            string rid = Util.FindPropertyValue(webresponse, "Id");

            result = GetFailedActivities(rid);

            Util.WriteToFile(result, path);

        }
    }
}
